<template>
    <div class="Jcommon-layout">
        <div class="Jcommon-layout-center">
            <el-row class="Jcommon-search-box" :gutter="16">
                <el-form :model="queryParams" ref="queryForm" v-show="showSearch">
                    <el-col :span="6">
                        <el-form-item label="车牌号" prop="plateCode">
                            <el-input v-model="queryParams.plateCode" placeholder="请输入车牌号" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="车位编号" prop="carCode">
                            <el-input v-model="queryParams.carCode" placeholder="请输入车位编号" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="车位状态" prop="status">
                            <el-select v-model="queryParams.status" placeholder="请选择车位状态">
                                <el-option :key="1" label="正常" :value="1"></el-option>
                                <el-option :key="0" label="到期" :value="0"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="业主名称" prop="userName">
                            <el-input v-model="queryParams.userName" placeholder="请输入业主名称" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="联系方式" prop="phonenumber">
                            <el-input v-model="queryParams.phonenumber" placeholder="请输入联系方式" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="Jcommon-layout-main Jflex-main">
                <div class="Jcommon-head">
                    <el-row :gutter="10" class="mb8">
                        <el-col :span="1.5">
                            <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
                        </el-col>
                    </el-row>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </div>

                <JTable v-loading="loading" :data="parkdingOwerCarList">
                    <el-table-column label="车牌号" align="center" prop="plateCode" />
                    <!-- <el-table-column label="房屋号" align="center" prop="" /> -->
                    <el-table-column label="车辆类型" align="center" prop="carType">
                        <template slot-scope="scope">
                            <dict-tag :options="dict.type.parkingownercar_cartype" :value="scope.row.carType" />
                        </template>
                    </el-table-column>
                    <el-table-column label="颜色" align="center" prop="carColor" />
                    <el-table-column label="业主" align="center" prop="userName" />
                    <el-table-column label="车位" align="center" prop="carCode" />
                    <el-table-column label="有效期" align="center" width="230">
                        <template slot-scope="scope">
                            <span>{{ scope.row.startTime }} ~ {{ scope.row.endTime }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" align="center" prop="status">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.status === 1">正常</el-tag>
                            <el-tag v-else type="danger">到期</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="160">
                        <template slot-scope="scope">
                            <el-button style="color: #67c23a" v-if="scope.row.seatStatus == 1" type="text" @click="handleUpdateSf(scope.row)">释放</el-button>
                            <el-button style="color: #e6a23c" type="text" @click="handleUpdateXZ(scope.row)">续租</el-button>
                            <el-button type="text" @click="handleUpdate(scope.row)">修改</el-button>
                            <el-button type="text" class="JTable-delBtn" @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </JTable>
                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
            </div>
        </div>

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
            <el-steps align-center :active="active" finish-status="success" style="margin-bottom: 25px">
                <el-step title="选择车位"></el-step>
                <el-step title="业主信息"></el-step>
                <el-step title="车辆信息"></el-step>
            </el-steps>
            <el-card class="seat-card" v-if="active === 0">
                <div slot="header" class="clearfix">
                    <span>停车位信息</span>
                    <el-button style="float: right" type="primary" icon="el-icon-search" @click="handleSeat(1)">选择车位</el-button>
                </div>
                <el-row>
                    <el-col :span="8">
                        <div class="seat-item">车位ID: {{ seatInfo.id }}</div>
                    </el-col>
                    <el-col :span="8">
                        <div class="seat-item">编号:{{ seatInfo.carCode }}</div>
                    </el-col>
                    <el-col :span="8">
                        <div class="seat-item">停车场: {{ seatInfo.parkingCode }}</div>
                    </el-col>
                    <el-col :span="8">
                        <div class="seat-item">
                            车位状态:
                            <dict-tag style="display: inline-block" :options="dict.type.park_space_status" :value="seatInfo.status" />
                        </div>
                    </el-col>
                    <el-col :span="8">
                        <div class="seat-item">面积: {{ seatInfo.area }} 平方米</div>
                    </el-col>
                    <el-col :span="8">
                        <div class="seat-item">备注: {{ seatInfo.remarks }}</div>
                    </el-col>
                </el-row>
            </el-card>
            <el-card class="seat-card" v-if="active === 1">
                <div slot="header" class="clearfix">
                    <span>业主信息</span>
                    <el-button style="float: right" type="primary" icon="el-icon-search" @click="handleSeat(2)">选择业主</el-button>
                </div>
                <el-row>
                    <el-col :span="8">
                        <div class="seat-item">业主ID: {{ userInfo.id }}</div>
                    </el-col>
                    <el-col :span="8">
                        <div class="seat-item">名称:{{ userInfo.userName }}</div>
                    </el-col>
                    <el-col :span="8">
                        <div class="seat-item">
                            性别:
                            <dict-tag style="display: inline-block" :options="dict.type.sys_user_sex" :value="userInfo.sex" />
                        </div>
                    </el-col>
                    <el-col :span="8">
                        <div class="seat-item">身份证: {{ userInfo.idcard }}</div>
                    </el-col>
                    <el-col :span="8">
                        <div class="seat-item">联系方式: {{ userInfo.phonenumber }}</div>
                    </el-col>
                    <el-col :span="8">
                        <div class="seat-item">备注: {{ userInfo.remark }}</div>
                    </el-col>
                </el-row>
            </el-card>
            <el-card class="seat-card" v-if="active === 2">
                <addorUpdate v-if="formVisible3" ref="addForm" @submitForm="submitForm" :fuForm="ownercarForm" :title="title"></addorUpdate>
            </el-card>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel()">取 消</el-button>
                <template v-if="active === 0">
                    <el-button type="primary" @click="next1">下一步</el-button>
                </template>
                <template v-if="active === 1">
                    <el-button type="success" @click="active = 0">上一步</el-button>
                    <el-button type="primary" @click="next2">下一步</el-button>
                </template>
                <template v-if="active === 2">
                    <el-button type="success" @click="active = 1">上一步</el-button>
                </template>
            </div>
        </el-dialog>
        <!--选择车位的 弹框-->
        <select-seat v-if="formVisible" ref="selectSeatForm" @handleSelectSeatInfo="handleSelectSeatInfo"></select-seat>
        <!--选择业主的 弹框-->
        <select-user v-if="formVisible2" ref="selectUserForm" @handleSelectUserInfo="handleSelectUserInfo"></select-user>
        <!--续租-->
        <xz-modal v-if="formVisible4" ref="xzForm" @refreshDataList="handleQuery"></xz-modal>
    </div>
</template>

<script>
import { getParkdingOwerCarDetail, getParkdingOwerCarList, addParkdingOwerCar, updateParkdingOwerCar, delParkdingOwerCar, sfParkdingOwerCar } from '@/api/parking/parkingOwnerCar.js';
import selectSeat from './modal/select-seat.vue';
import selectUser from './modal/select-user.vue';
import addorUpdate from './modal/addorUpdate';
import xzModal from './modal/xz-modal.vue';

export default {
    dicts: ['parkingownercar_cartype', 'park_space_status', 'sys_user_sex'],
    components: { selectSeat, selectUser, addorUpdate, xzModal },
    data() {
        return {
            active: 0, //
            formVisible: false,
            seatInfo: {},
            userInfo: {},
            formVisible2: false,
            formVisible3: false,
            formVisible4: false,
            // 遮罩层
            loading: true,
            id: '',
            // 导出遮罩层
            // exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            // single: true,
            // 非多个禁用
            // multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 列表数据
            parkdingOwerCarList: [],

            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                page: 1,
                limit: 10,
                plateCode: '',
                carCode: '',
                status: null,
                userName: '',
                phonenumber: '',
            },
            // 表单参数
            form: {},
            // 车辆信息
            ownercarForm: { id: '' },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        // 步骤一
        next1() {
            if (Object.keys(this.seatInfo).length == 0) {
                return this.$modal.msgWarning('请选择一个车位');
            }
            this.active = 1;
        },
        // 步骤二
        next2() {
            if (Object.keys(this.userInfo).length == 0) {
                return this.$modal.msgWarning('请选择一个业主');
            }
            this.active = 2;
            this.formVisible3 = true;
        },
        // 选择车位/选择业主 打开弹框
        handleSeat(index) {
            if (index === 1) {
                this.formVisible = true;
            }
            if (index === 2) {
                this.formVisible2 = true;
            }
            this.$nextTick(() => {
                if (index === 1) {
                    this.$refs.selectSeatForm.init();
                } else {
                    this.$refs.selectUserForm.init();
                }
            });
        },
        // refreshDataList() {
        //     this.getList();
        // },
        handleSelectSeatInfo(row) {
            this.seatInfo = row;
            console.log('选择了停车位', this.seatInfo);
        },
        handleSelectUserInfo(row) {
            this.userInfo = row;
            console.log('选择了业主信息', this.userInfo);
        },
        // /** 修改按钮操作 */
        handleUpdate(row) {
            this.active = 0;
            this.reset();
            this.open = true;
            this.title = '修改信息';
            this.id = row.id; // || this.ids
            getParkdingOwerCarDetail(this.id).then(response => {
                let data = response.data;
                // 车位信息
                this.seatInfo = { ...data };
                this.seatInfo.id = data.seatId;
                this.seatInfo.status = data.seatStatus;
                // 业主信息
                this.userInfo = { ...data };
                this.userInfo.id = data.ownerId;
                // 车辆信息
                this.ownercarForm = { ...data };
            });
        },
        // 提交了数据
        submitForm(form) {
            // this.ownercarForm = form;
            let params = {
                parkingId: this.seatInfo.parkingId,
                seatId: this.seatInfo.id,
                ownerId: this.userInfo.id,
                plateCode: form.plateCode,
                carType: form.carType,
                carColor: form.carColor,
                startTime: form.startTime,
                endTime: form.endTime,
                brand: form.brand,
                id: this.id,
                remarks: form.remarks,
            };
            // console.log('提交数据？？？', params);
            if (this.id) {
                updateParkdingOwerCar(params).then(response => {
                    this.$modal.msgSuccess('修改成功');
                    this.open = false;
                    this.getList();
                });
            } else {
                addParkdingOwerCar(params).then(response => {
                    this.$modal.msgSuccess('新增成功');
                    this.open = false;
                    this.getList();
                });
            }
        },
        /** 查询 列表 */
        getList() {
            this.loading = true;
            getParkdingOwerCarList(this.queryParams).then(response => {
                this.parkdingOwerCarList = response.data.list;
                this.total = response.data.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.active = 0;
            this.formVisible = false;
            this.formVisible2 = false;
            this.formVisible3 = false;
            this.reset();
            // console.log('取消按钮？？');
        },
        // 表单重置
        reset() {
            // console.log('重置按钮？？');
            this.seatInfo = {};
            this.userInfo = {};
            this.ownercarForm = { id: '' };
        },
        // /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        // /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // // 多选框选中数据
        // // handleSelectionChange(selection) {
        // //     this.ids = selection.map(item => item.id);
        // //     this.single = selection.length !== 1;
        // //     this.multiple = !selection.length;
        // // },
        // /** 新增按钮操作 */
        handleAdd() {
            this.active = 0;
            this.reset();
            this.open = true;
            this.title = '添加信息';
        },
        // 续租
        handleUpdateXZ(row) {
            this.formVisible4 = true;
            this.$nextTick(() => {
                this.$refs.xzForm.id = row.id;
                this.$refs.xzForm.init();
            });
        },
        // 释放
        handleUpdateSf(row) {
            const ids = row.id; // || this.ids
            let params = {
                seatId: row.seatId,
                id: ids,
            };
            this.$modal
                .confirm('是否确认释放车位编号为"' + ids + '"的数据项？')
                .then(function () {
                    return sfParkdingOwerCar(params);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('释放成功');
                })
                .catch(() => {});
        },
        // /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除楼栋管理编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delParkdingOwerCar(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => {});
        },
    },
};
</script>
<style scoped>
.seat-card {
}
.seat-item {
    padding: 10px 0;
}
</style>
